package com.qf.homework.controller;

import com.qf.homework.service.UserService;
import com.qf.homework.service.impl.UserServiceImpl;
import com.qf.homework.utils.ResponseUtils;
import com.qf.homework.utils.StatUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/User/register")
public class RegisterController extends HttpServlet {
    private UserService service=new UserServiceImpl();
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if (username==null||password==null){ ResponseUtils.nullParamDetai(resp, StatUtil.NULL_ACCOUNT); }
        try {
            int res = service.register(username, password);
            if (res==0) {ResponseUtils.fail(resp,StatUtil.ACCOUNT_EXIST);}
            else if (res==1){ResponseUtils.success(resp,StatUtil.SUCCESS);}
            else {throw new RuntimeException("发生重复注册");}
        } catch (SQLException troubles) {
            troubles.printStackTrace();
        }
    }
}
